package com.value.utils;

import java.security.MessageDigest;

public class Md5Encoder {
	public static String generateCode(String source){
		if(source==null||"".equals(source)){
			return "";
		}
		byte[] sourceByte = source.getBytes();
		String target = null;
		char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',  'e', 'f'}; 
		try{
		    MessageDigest md = MessageDigest.getInstance( "MD5" );
		    md.update( sourceByte );
		    byte tmp[] = md.digest();
		    char str[] = new char[16 * 2];
		    int k = 0;
		    for (int i = 0; i < 16; i++){
			    byte byte0 = tmp[i];
			    str[k++] = hexDigits[byte0 >>> 4 & 0xf];
			    str[k++] = hexDigits[byte0 & 0xf];
		    } 
		    target = new String(str);
		    return target;
		}catch( Exception e ){
		    e.printStackTrace();
		}
		return ""; 
	}
}
